/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sct_beans;

import entities.TestResult;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.jms.*;

/**
 *
 * @author ryuju
 */
@Stateless
@LocalBean
public class TestResultMsgSender {
    @Resource(mappedName = "jms/TestResultMsgQueue")
    private Queue testResultMsgQueue;
    @Resource(mappedName = "jms/TestResultMsgQueueFactory")
    private ConnectionFactory testResultMsgQueueFactory;
    
    public void sendTestResultMsg(TestResult tr)
    {
        try {
            
            sendJMSMessageToTestResultMsgQueue(tr);
        } catch (JMSException ex) {
            Logger.getLogger(TestResultMsgSender.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private Message createJMSMessageForjmsTestResultMsgQueue(Session session, TestResult messageData) throws JMSException {
        // TODO create and populate message to send
        ObjectMessage om = session.createObjectMessage();
        om.setObject(messageData);
        return om;
    }

    private void sendJMSMessageToTestResultMsgQueue(TestResult messageData) throws JMSException {
        Connection connection = null;
        Session session = null;
        try {
            connection = testResultMsgQueueFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = session.createProducer(testResultMsgQueue);
            messageProducer.send(createJMSMessageForjmsTestResultMsgQueue(session, messageData));
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException e) {
                    Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Cannot close session", e);
                }
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}
